package utils
{
	/**
	 *@date 2016-5-31 上午11:21:19
	 *@author zhouhonghui
	 */
	public class StrUtil
	{
		public static var SPACE_REG:RegExp=/\s/g;
		public static var STR_REG:RegExp = /{[0-9]+[\s]?}/
		public static var OBJ_REG:RegExp = /{[0-9]+\.{1}\[\w+\]}/
		public static var ARR_REG:RegExp = /{[0-9]+:{1}\[\d+\]}/
		public static var reg1:RegExp = /{.*?}/g; //? 问号的作用是取消 .* 的贪婪匹配 遇到}就结束
		
		/*
		1.什么是正则表达式的贪婪与非贪婪匹配
		
		　　如：String str="abcaxc";
		
		　　　　Patter p="ab*c";
		
		　　贪婪匹配:正则表达式一般趋向于最大长度匹配，也就是所谓的贪婪匹配。如上面使用模式p匹配字符串str，结果就是匹配到：abcaxc(ab*c)。
		
		　　非贪婪匹配：就是匹配到结果就好，就少的匹配字符。如上面使用模式p匹配字符串str，结果就是匹配到：abc(ab*c)。
		
		2.编程中如何区分两种模式
		
		　　默认是贪婪模式；在量词后面直接加上一个问号？就是非贪婪模式。
		
		　　量词：{m,n}：m到n个
		
		　　　　　*：任意多个
		
		　　　　　+：一个到多个
		
		　　　　　？：0或一个 
		
		 */
		
		public function StrUtil()
		{
		}
		
		/**
		 * 把字符串复杂的字符串转换成数组 
		 * 例如:</br>
		 * exStr1=1001,101,10|2001,201,20  这样的字符串</br>
		 * exStr2=1001,101,10#1002,101,11|2001,201,20#1002,101,11这样的字符串
		 * @param srcStr    要转换的字符串
		 * @param splitChar 分隔符(多个分隔符)
		 * @param keys		object的key列表
		 * @param autoTran	是否自动转换数据类型(主要是纯数字的串转换成number)默认转
		 * @param objClass	key的渲染类
		 * @return array
		 * @example 
		 * strToObjArr(exStr1,"|,",["id","type","num"]) == [{},{}] 这种用得比较多
		 * strToObjArr(exStr2,"|#,",["id","type","num"]) == [[{},{}],[{},{}]]
		 */		
		public static function strToObjArr(srcStr:String,splitChar:String="|,",objKeys:Array=null,autoTran:Boolean=true,objClass:Class=null):Array
		{
			var ret:Array;
			
			if(!splitChar || splitChar=="")
			{
				ret = [srcStr];
			}else if(splitChar.length==1)
			{
				ret = srcStr.split(splitChar);			
			}else
			{
				var dim:int = splitChar.length;
				ret = toSplitStr(srcStr,splitChar,objKeys,autoTran,objClass);				
			}			
			return ret;	
		}
		/**解析字符串**/
		private static function toSplitStr(srcStr:String,splitChar:String="|,",objKeys:Array=null,autoTran:Boolean=true,objClass:Class=null):*
		{
			var ret:Array;			
			var split:String="";			
			var i:int,j:int,index:int=0;
			var obj:Object;
			var tem:Array;
			if(!splitChar || splitChar=="")
			{
				ret = [srcStr];
			}else
			{
				split = splitChar.charAt(0);				
				var len:int = 0;
				if(splitChar.length>1)
				{
					ret = srcStr.split(split);
					len = ret.length;
					for (i = 0; i < len; i++) 
					{
						ret[i] = toSplitStr(ret[i],splitChar.substr(1),objKeys,autoTran,objClass);
					}					
				}else if(splitChar.length==1)
				{
					tem = srcStr.split(split);
					len = tem.length;
					if(objKeys)
					{
						objClass!=null?obj = new objClass():obj = {};						
						for (i = 0; i < len; i++) 
						{
							autoTran && !isNaN(Number(tem[i]))?tem[i] = Number(tem[i]):""; //把数值字符串转换成number
							objKeys[i]?obj[objKeys[i]] = tem[i]:obj["__key__"+i] = tem[i];							
						}
						return obj;
					}else
					{
						return tem;
					}					
				}				
			}			
			return ret;	
		}
		
		/**
		 * 根据多个参数替换 
		 * @param srcStr 要替换的字符串(必须是类似: "将消耗{0}元宝增加{1}次挑战次数" 这样的字符串 )
		 * @param arg
		 * @return 
		 * 
		 */		
		public static  function repByArgs(srcStr:String,...arg):String
		{
			for (var i:int = 0; i < arg.length; i++) 
			{
				srcStr = srcStr.replace("{"+i+"}",arg[i].toString());
			}
			
			return srcStr;
		}
		/**
		 * 根据参数格式化字符串 
		 * @param src
		 * @param args
		 * @return 
		 * @example		 
		 * s = format('恭喜【{0}】成功升级到了{1.[lv]},将会获得{2:[1]}经验',"哈哈大将军",{lv:59},[1500,512])
		 * trace(s);// 恭喜【哈哈大将军】成功升级到了59,将会获得512经验
		 */		
		public static function format(src:String,...args):String
		{
			while(true)
			{
				var str:String = STR_REG.exec(src)
				var objStr:String = OBJ_REG.exec(src);
				var arrStr:String = ARR_REG.exec(src);				
				var idx:int;
				var idxTempStr:String;
				var arr:Array;
				if(objStr)
				{
					arr = getArgArr(objStr,'.');			
					idx = getIdx(arr[0]);
					var pro:String = arr[1].replace(/\[|\]/g,"");
					pro = pro.replace(/{|}/g,"");					
					src = src.replace(OBJ_REG,args[idx][pro]);
				}else if(arrStr)
				{
					arr = getArgArr(arrStr,":")					
					idx = getIdx(arr[0]);
					var argIdxStr:String = arr[1].replace(/\[|\]/g,"");
					argIdxStr = argIdxStr.replace(/{|}/g,"");
					var argsIndx:int = int(argIdxStr);
					src = src.replace(ARR_REG,args[idx][argsIndx]);
				}else if(str)
				{
					str = str.replace(SPACE_REG,"");
					str = str.replace(/{|}/g,"");
					idx = int(str);
					src = src.replace(STR_REG,args[idx]);
				}else
				{
					break;
				}
				
			}
			
			return src;
		}
		private static function getArgArr(src:String,delimit:String):Array
		{
			src = src.replace(SPACE_REG,"");
			var arr:Array = src.split(delimit);
			return arr;
		}
		private static function getIdx(idxTempStr:String):int
		{	
			idxTempStr = idxTempStr.replace(/{|}/g,"");
			return int(idxTempStr);
		}
		
		public static function strToArrArgs(input:String,itemRcs:Array=null):String
		{
			if(itemRcs)
			{
				var idx:int = 0;
				while(idx<itemRcs.length)
				{
					var itmStr:String = itemRcs[idx];
					var fidx:int=input.indexOf(itmStr);
					if(fidx>-1)
					{
						var headStr:String = input.substring(0,fidx);
						var tempIdx:int = fidx+itmStr.length;
						input=headStr+"{0:"+"["+idx+"]}"+input.substring(tempIdx,input.length);
					}
				}
			}
			return input;
		}
		
		/**
		 * 格式化自定义的链接字符串，并返回html文本
		 * @param s   要格式化的字符串(类似:<{href="@recharge"完成首次充值}> 这样的字符串)
		 * @param clr 文本颜色
		 * @return 
		 * 
		 */		
		public static function formatDescLink(s:String,clr:uint):String
		{
			var attrReg:RegExp = RegExpUtil.ATTR_BLOCK_REG;
			var reg0:RegExp = RegExpUtil.BRACE;			
			var reg1:RegExp = RegExpUtil.BRACE_CONTENT;			
			var obj:Object = reg1.exec(s);			
			var subStr:String = "";			
			if(!obj)
			{
				subStr =HtmlUtil.makeHtml(s,clr);
			}else
			{
				//有超链接
				var stIdx:int = 0;
				while(obj)
				{
					var ms:String = obj[0];
					var temp:String = s.substring(stIdx,obj.index);
					if(temp!="")
					{
						temp = HtmlUtil.makeHtml(temp,clr);
					}
					stIdx = obj.index+ms.length;
					var ma:Array = s.split(reg1);
					ms = ms.replace(reg0,""); 
					var richLink:String = ms.match(attrReg)[0];
					var islink:Boolean = false;
					if(richLink)
					{
						richLink = richLink.replace(/\"|\"|(href=)/ig,"");
						islink = true;
					}else
					{
						richLink = "";
					}
											
					ms = ms.replace(attrReg,"");
					ms = HtmlUtil.makeHtml(ms,clr,12,islink,"",richLink); //生成超链接					
					subStr+=temp+ms;
					s = s.substring(stIdx,s.length);
					stIdx = 0;
					obj = reg1.exec(s);					
				}
				if(s!="")
				{
					subStr+=HtmlUtil.makeHtml(s,clr);;
				}
			}			
			return subStr;
		}
		
		
	}
}